const util = require("../../../utils/util");
import Toast from "../../../vant/weapp/toast/toast";
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    noticeshow: false,
    content: '',
    vip: 0,
    pricecouponList: '[]',
    cardcouponList: '[]',
    store_name: '',
    couponsum: '',
    couponshow: false,
    couponshowif: 1,
    store: [],
    index: 0, //房间
    cardList: [],
    date_start: "",
    time_start: "", //开始
    date_end: "",
    time_end: "",
    time: 0, //套餐时间
    package_id: 0,
    privatestore: [],
    chooseIndex: -1,
    store_id: 0, //门店id
    is_user_show: true,
    is_user: 0,
    showUser: false,
    show: false,
    img: [],
    isList: true,
    swiperH: '400rpx',
    indexList: [],
    coupon: '',
    balance: 0,
    preList: [{
        url: "/image/chair.png",
        name: "本店余额",
        subtitle: "本店快速充值",
        link: "/pages/person/vipplas/index",
      }, {
        url: "/image/joinus.png",
        name: "新人福利",
        subtitle: "老板发放福利啦",
        link: "/pages/present/index",
      },
      {
        url: "/image/meituan.png",
        name: "美团兑换",
        subtitle: "本店快速充值",
        link: "/pages/index/meituan/index",
      }, {
        url: "/image/douyin.png",
        name: "抖音兑换",
        subtitle: "老板发放福利啦",
        link: "/pages/index/meituan/index",
      },

    ],

  },
  onShareAppMessage() {
    const that = this
    console.log(that.data.store_id)
    console.log(wx.getStorageSync("uid"))
    return {
      title: '会员邀请新人下单送福利啦',
      desc: '新人下单送30余额',
      path: '/pages/index/index?store_id=' + that.data.store_id + '&uid=' + wx.getStorageSync("uid"), // 路径，传递参数到指定页面。
    }
  },
  onLoad(options) {
    const that = this
    if (options.store_id) {
      that.setData({
        store_id: options.store_id
      })
      that.getstore()
      that.getroomlist(options.store_id)
      return
    }
    // if (options.scene != undefined) {
    //   const scene = decodeURIComponent(options.scene)
    //   let arrPara = scene.split('&');
    //   let arrPara1 = []
    //   console.log(arrPara)
    //   for (let i = 0; i < arrPara.length; i++) {
    //     arrPara1.push(arrPara[i].split('='))
    //   }
    //   that.setData({
    //     store_id: arrPara1[1][1]
    //   })
    //   that.getstore()
    //   that.getroomlist(arrPara1[1][1])
    //   return
    // }

  },
  hide_notice() {
    this.setData({
      noticeshow: false
    })
  },
  hide_coupon() {

    this.setData({
      couponshow: false,
      couponshowif: 0
    })

    wx.setStorageSync('couponshow' + this.data.store_id, 1)
    console.log(this.data.store_id)
    wx.navigateTo({
      url: '/pages/present/index?store_id=' + this.data.store_id,
    });
  },
  onShow: function () {
    const that = this
    //that.getUserOrderInfo()
  },
  // 跳转
  linkTo(e) {
    console.log(e, this.data.store_id);
    const url = e.currentTarget.dataset.link;
    console.log(`${url}?store_id=${this.data.store_id}`)
    if (e.currentTarget.dataset.name == '美团兑换') {
      wx.navigateTo({
        url: `${url}?tab=1&store_id=${this.data.store_id}`,
      });
    } else if (e.currentTarget.dataset.name == '抖音兑换') {
      wx.navigateTo({
        url: `${url}?tab=2&store_id=${this.data.store_id}`,
      });
    } else {
      wx.navigateTo({
        url: `${url}?store_id=${this.data.store_id}`,
      });
    }


  },

  chooseTime(e) {

    const id = e.currentTarget.dataset.id;
    const type = e.currentTarget.dataset.type;
    wx.navigateTo({
      url: "/pages/person/pricecoupon/index?id=" + id + "&type=" + type,
    });
  },

  // 打电话
  call() {
    const that = this
    let mobile = that.data.store[that.data.index].mobile
    wx.makePhoneCall({
      phoneNumber: mobile
    })
  },
  // 导航
  duo_map() {
    const that = this;
    let data = that.data.store[that.data.index]
    wx.getLocation({
      type: "wgs84",
      success: function (res) {
        wx.openLocation({
          //​使用微信内置地图查看位置。
          latitude: Number(data.lat), //要去的纬度-地址
          longitude: Number(data.lng), //要去的经度-地址
          name: data.name,
          address: data.address,
          scale: 18,
        });
      },
      fail(e) {
        console.log(e);
        wx.setStorageSync("openSet", true);
        that.openSet();
      },
    });
  },
  //   登录
  login() {
    console.log('login')
    if (wx.getStorageSync('token') == "") {
      this.setData({
        is_user_show: false,
        is_user: 1
      });
    }
  },
  // 登录遮罩层
  close(e) {
    this.setData({
      is_user_show: e.detail,
      is_user: 0
    });
  },
  // 登陆后更新
  renew(e) {
    console.log(e)
    this.setData({
      showUser: true,
    });
    wx.setStorageSync('userInfo', e.detail)
  },
  // 门店列表
  getstore() {
    const that = this
    app.http.get(app.api.store, {}, (res) => {
      that.setData({
        store: res.data.lists
      })
      if (that.data.store_id == 0) {
        let store_id = that.data.store[that.data.index].store_id.toString()
        that.setData({
          store_id,
          imgUrls: that.data.store[that.data.index].head_url
        })
      } else {
        let stores = that.data.store;
        for (let i in stores) {
          if (stores[i].store_id == that.data.store_id) {
            that.setData({
              index: i,
              imgUrls: stores[i].head_url
            })
            return
          }
        }
      }
    })
  },
  // 包厢列表
  getroomlist(store) {
    const that = this
    // 优惠卷列表
    app.http.get(app.api.cardcoupon, {
      store_id: store
    }, (res) => {
      this.setData({
        cardcouponList: res.data.lists ? res.data.lists : [],
      })
    })
    let add_time = that.data.date_start + " " + that.data.time_start;
    let store_id
    if (store) {
      store_id = store
    } else {
      store_id = that.data.store[that.data.index].store_id.toString()
    }
    //包厢列表
    app.http.get(app.api.roomlist, {
      store_id
    }, (res) => {
      let newPreList = this.data.preList || []; // 确保 preList 是数组
      if (newPreList.length > 0) {
        newPreList[0].name = '余额:' + res.data.balance;
      } else {
        newPreList.push({
          name: '余额:' + res.data.balance
        });
      }
      that.setData({
        privatestore: res.data.lists,
        balance: res.data.balance,
        content: res.data.notice,
        isList: false,
        noticeshow: res.data.notice == '' ? false : true,
        preList: newPreList
      })

    })
    // 



  },
  // 改图片
  changeImg(e) {
    const that = this;

    let img = e.currentTarget.dataset.img
    console.log(img)
    let imgs = JSON.stringify(img)
    if (img.length <= 0) {
      return
    }
    wx.navigateTo({
      url: '/pages/index/swiper/index?img=' + imgs,
    })
  },
  // 切换门店
  bindPickerChange(e) {
    const that = this
    that.setData({
      chooseIndex: -1,
      index: e.detail.value,
      store_id: that.data.store[e.detail.value].store_id,
      imgUrls: that.data.store[e.detail.value].head_url
    })
    that.getroomlist(that.data.store[e.detail.value].store_id)
    // that.getPackage()
    that.getstore()
  },
  // 首页开门
  getUserOrderInfo() {
    const that = this
    if (wx.getStorageSync('token') == "") {
      return
    }
    app.http.post(app.api.userOrderInfo, {}, (res) => {
      if (res.data == "") {
        return
      }
      if (res.data[0].order === 1) {
        that.setData({
          indexList: res.data[0]
        })
      } else {
        that.setData({
          indexList: {
            door_list: [{
              door_name: "开门"
            }],
          }
        })
      }
    })
  },


  onShareTimeline() {}
});